package user

/**
 * Author       : JingHua Fan
 * Date         : 2021-11-28 20:40:47
 * LastEditTime : 2021-12-24 16:56:32
 * LastEditors  : JingHua Fan
 * Description  : 佛祖保佑,永无BUG
 */
import (
	"coupons/pkg/logger"
	"coupons/pkg/model"
	"coupons/pkg/types"
)

// Create 创建用户, 通过 User.ID 来判断是否创建成功
func (user *User) Create() (err error) {
	if err = model.DB.Create(&user).Error; err != nil {
		logger.LogError(err)
		return err
	}
	return nil
}

// Get 通过ID获取用户
func Get(idstr string) (User, error) {
	var user User
	id := types.StringToUint64(idstr)
	if err := model.DB.First(&user, id).Error; err != nil {
		return user, err
	}
	return user, nil
}

// GetByEmail 通过 Email 来获取用户
func GetByEmail(email string) (User, error) {
	var user User
	if err := model.DB.Where("email = ?", email).First(&user).Error; err != nil {
		return user, err
	}
	return user, nil
}
